<?php
// Twitter Model
// Uses the Twitter API v1.1
// updated: 2013/06/17

class Twitter {
  var $screen_name;
  var $slug;
  var $access_token;
  var $error = false;
  var $error_message;
  var $tweets;

  public function __construct($params = NULL) {
    $this->screen_name = isset($params['screen_name']) ? $params['screen_name'] : NULL;
    $this->slug = isset($params['slug']) ? $params['slug'] : NULL;
    $this->access_token = isset($params['access_token']) ? $params['access_token'] : NULL;

    // Init a CURL instance
    $this->ch = curl_init();
    curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
  }

  public function listStatuses($count=3) {
    $ApiUrl = "https://api.twitter.com/1.1/lists/statuses.json";

    curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($this->ch, CURLOPT_URL, "$ApiUrl?slug=".$this->slug."&owner_screen_name=" . $this->screen_name . "&count=$count");
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->access_token));

    return $this;
  }

  public function userTimeline($count = 3) {
    $ApiUrl = "https://api.twitter.com/1.1/statuses/user_timeline.json";

    curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($this->ch, CURLOPT_URL, "$ApiUrl?screen_name=" . $this->screen_name . "&count=$count");
    curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer " . $this->access_token));

    return $this;
  }

  public function get() {
    $data = curl_exec($this->ch);
    $curl_error = curl_error($this->ch);
    curl_close($this->ch);

    // check for curl errors
    if($curl_error) {
      $this->error = 1;
      $this->error_mesage = $curl_error;
    }

    $obj = json_decode($data);

    // check for twitter api errors
    if(isset($obj->errors)) {
      $this->error = 1;
      $this->error_message = $obj->errors[0]->message;
    }

    if(!$this->error) {
      $this->tweets = $obj;
      return $this->parse();
    }

    return $this->error_message;
  }

  private function parse() {
    $hashtagUrl = "<a href=\"https://twitter.com/search?q=%%23%s&src=hash\" target=\"_blank\">%s</a>";
    $userMentions = '<a href="https://twitter.com/%s" target="_blank">%s</a>';
    $webUrl = '<a href="%s" target="_blank">%s</a>';

    foreach($this->tweets as $tweet) {
      $text = $tweet->text;

      // look for hash tags
      if(count($tweet->entities->hashtags)) {
        foreach($tweet->entities->hashtags as $hash) {
          $text = str_replace('#' . $hash->text, sprintf($hashtagUrl, $hash->text, '#' . $hash->text), $text);
        }
      }

      // look for user mentions
      if(count($tweet->entities->user_mentions)) {
        foreach($tweet->entities->user_mentions as $user) {
          $text = str_replace('@' . $user->screen_name, sprintf($userMentions, $user->screen_name, '@' . $user->screen_name), $text);
        }
      }

      // look for URLs
      if(count($tweet->entities->urls)) {
        foreach($tweet->entities->urls as $link) {
          $text = str_replace($link->url, sprintf($webUrl, $link->url, $link->display_url), $text);
        }
      }

      // look for Media
      if(isset($tweet->entities->media) && count($tweet->entities->media)) {
        foreach($tweet->entities->media as $link) {
          $text = str_replace($link->url, sprintf($webUrl, $link->url, $link->display_url), $text);
        }
      }

      $tweet->richtext = $text;
    }

    return $this->tweets;
  }
}

